/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;

import com.bourre.transitions.TweenMS;

import fever.display.effects.TweenEngine;

import fvaswing.effects.easing.Strong;
import fvaswing.effects.FvTweenEffect;

/**
 * Fade effect on <strong>AsWing components</strong>
 * 
 * <p>Animates the alpha property of a component, either from transparent to 
 * opaque, or from opaque to transparent.
 * 
 * <p>Example
 * {@code
 *   public function test() : Void
 *   {
 *     var button : JButton = new JButton( "move button" );
 *     
 *     var effect : FvFade = new FvFade( button );
 *     effect.addEventListener( FvEffectEvent.onEffectEndEVENT, this, _onEnd );
 *     effect.alphaTo = 0;
 *     
 *     effect.play();
 *   }
 * }
 * @author Romain Ecarnot
 */
class fvaswing.effects.FvFade extends FvTweenEffect
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------

	private var _startAlpha : Number;
	private var _endAlpha : Number;	private var _storeAlpha : Number;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/**
	 * Initial transparency level between 0 and 100, where 0 means transparent 
	 * and 100 means fully opaque.
	 */
	public var alphaFrom : Number;
	
	/**
	 * Final transparency level, where 0 means transparent and 100 means fully 
	 * opaque.
	 */
	public var alphaTo : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns a predefined Fade effect where alpha property is animated
	 * from transparent to opaque.
	 * 
	 * @param target Component target
	 */
	public static function fadeIn( t : Component ) : FvFade
	{
		var e : FvFade = new FvFade( t );
		e.alphaFrom = 0;
		e.alphaTo = 100;
		e.easing = Strong.easeOut;
		return e;	
	}
	
	/**
	 * Returns a predefined Fade effect where alpha property is animated
	 * from opaque to transparent.
	 * 
	 * @param target Component target
	 */
	public static function fadeOut( t : Component ) : FvFade
	{
		var e : FvFade = new FvFade( t );
		e.alphaFrom = 100;
		e.alphaTo = 0;
		e.easing = Strong.easeOut;
		return e;	
	}
	
	/**
	 * Constructor.
	 * 
	 * @param t AsVing component target
	 */
	public function FvFade( t : Component )
	{
		super( t ) ;
		
		alphaFrom = Number.NaN;
		alphaTo = Number.NaN;
	}
	
	/**
	 * Starts effect.
	 * 
	 * <p>Stores current alpha for loop process.
	 */
	public function play() : Void
	{
		if( !_isPlaying )
		{
			_storeAlpha = _instance.alpha;
			
			super.play();
		}		
	}
	
	/**
	 * Ends effects.
	 * 
	 * <p>Overloads {@link FvTweenEffect#end} method.
	 */
	public function end() : Void
	{
		_tween.stop();
		
		_instance.alpha = _endAlpha;
		_instance.target.revalidateIfNecessary();
		
		_endEffect();
	}


	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Builds tweens instance
	 */
	private function _buildTween() : Void
	{
		_startAlpha = ( isNaN( alphaFrom ) ) ?  ( _isRepeated ) ? _storeAlpha : _instance.alpha : alphaFrom;
		_endAlpha = ( isNaN( alphaTo ) ) ?  100 : alphaTo;
		
		_tween = TweenEngine.getInstance().getBasicTween( _instance, 'alpha', _endAlpha, duration, _startAlpha, easing );
		_tween.addEventListener( TweenMS.onMotionFinishedEVENT, this );		_tween.addEventListener( TweenMS.onMotionChangedEVENT, this );
	}
}